'    Copyright (C) <2008>  <Kevin Pletinckx>
'
'    This program is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 3 of the License, or
'    (at your option) any later version.
'
'    This program is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.
'
'    You should have received a copy of the GNU General Public License
'    along with this program.  If not, see <http://www.gnu.org/licenses/>.

Imports System.Deployment
Public Class frmSettings
    'Loads the INI file.
    Dim objIniFile As New IniFile(My.Settings.INI_Location)
    Dim VCurrent As String = objIniFile.GetString("Settings", "Version_current", "")
    Dim VLatest As String = objIniFile.GetString("Settings", "Version_latest", "")
    'Loads the title
    Dim Title As String = objIniFile.GetString("Settings", "Title", "")
    'Loads the logo
    Dim Image As String = objIniFile.GetString("Settings", "Image", "")
    'Loads the button names.
    Dim TButton1 As String = objIniFile.GetString("Names", "Button1", "")
    Dim TButton2 As String = objIniFile.GetString("Names", "Button2", "")
    Dim TButton3 As String = objIniFile.GetString("Names", "Button3", "")
    Dim TButton4 As String = objIniFile.GetString("Names", "Button4", "")
    Dim TButton5 As String = objIniFile.GetString("Names", "Button5", "")
    Dim TButton6 As String = objIniFile.GetString("Names", "Button6", "")
    Dim TButton7 As String = objIniFile.GetString("Names", "Button7", "")
    Dim TButton8 As String = objIniFile.GetString("Names", "Button8", "")
    Dim TButton9 As String = objIniFile.GetString("Names", "Button9", "")
    Dim TButton10 As String = objIniFile.GetString("Names", "Button10", "")
    Dim TButton11 As String = objIniFile.GetString("Names", "Button11", "")
    Dim TButton12 As String = objIniFile.GetString("Names", "Button12", "")
    Dim TButton13 As String = objIniFile.GetString("Names", "Button13", "")
    Dim TButton14 As String = objIniFile.GetString("Names", "Button14", "")
    Dim TButton15 As String = objIniFile.GetString("Names", "Button15", "")
    'Loads the locations.
    Dim TLoc1 As String = objIniFile.GetString("Locations", "Button1_loc", "")
    Dim TLoc2 As String = objIniFile.GetString("Locations", "Button2_loc", "")
    Dim TLoc3 As String = objIniFile.GetString("Locations", "Button3_loc", "")
    Dim TLoc4 As String = objIniFile.GetString("Locations", "Button4_loc", "")
    Dim TLoc5 As String = objIniFile.GetString("Locations", "Button5_loc", "")
    Dim TLoc6 As String = objIniFile.GetString("Locations", "Button6_loc", "")
    Dim TLoc7 As String = objIniFile.GetString("Locations", "Button7_loc", "")
    Dim TLoc8 As String = objIniFile.GetString("Locations", "Button8_loc", "")
    Dim TLoc9 As String = objIniFile.GetString("Locations", "Button9_loc", "")
    Dim TLoc10 As String = objIniFile.GetString("Locations", "Button10_loc", "")
    Dim TLoc11 As String = objIniFile.GetString("Locations", "Button11_loc", "")
    Dim TLoc12 As String = objIniFile.GetString("Locations", "Button12_loc", "")
    Dim TLoc13 As String = objIniFile.GetString("Locations", "Button13_loc", "")
    Dim TLoc14 As String = objIniFile.GetString("Locations", "Button14_loc", "")
    Dim TLoc15 As String = objIniFile.GetString("Locations", "Button15_loc", "")

    Private Sub btnOpenFile1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFile1.Click
        'Opens filebrowser for the TopLogo image.
        OpenFileDialog1.ShowDialog()
        txtImage.Text = OpenFileDialog1.FileName
    End Sub

    Private Sub chkHideSettings_CheckedChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkHideSettings.CheckedChanged
        'If checked then hide, else show the Settings in taskbar.
        If chkHideSettings.Checked = True Then
            objIniFile.WriteString("Settings", "Hide_settings", "False")
            Me.ShowInTaskbar = False
        Else
            objIniFile.WriteString("Settings", "Hide_settings", "True")
            Me.ShowInTaskbar = True
        End If
    End Sub

    Private Sub cmbButtons_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbButtons.SelectedIndexChanged
        'Shows settings for Button 1
        If cmbButtons.Text = "Button 1" Then
            txtName.Text = TButton1
            txtLocation.Text = TLoc1
        End If
        'Shows settings for Button 2
        If cmbButtons.Text = "Button 2" Then
            txtName.Text = TButton2
            txtLocation.Text = TLoc2
        End If
        'Shows settings for Button 3
        If cmbButtons.Text = "Button 3" Then
            txtName.Text = TButton3
            txtLocation.Text = TLoc3
        End If
        'Shows settings for Button 4
        If cmbButtons.Text = "Button 4" Then
            txtName.Text = TButton4
            txtLocation.Text = TLoc4
        End If
        'Shows settings for Button 5
        If cmbButtons.Text = "Button 5" Then
            txtName.Text = TButton5
            txtLocation.Text = TLoc5
        End If
        'Shows settings for Button 6
        If cmbButtons.Text = "Button 6" Then
            txtName.Text = TButton6
            txtLocation.Text = TLoc6
        End If
        'Shows settings for Button 7
        If cmbButtons.Text = "Button 7" Then
            txtName.Text = TButton7
            txtLocation.Text = TLoc7
        End If
        'Shows settings for Button 8
        If cmbButtons.Text = "Button 8" Then
            txtName.Text = TButton8
            txtLocation.Text = TLoc8
        End If
        'Shows settings for Button 9
        If cmbButtons.Text = "Button 9" Then
            txtName.Text = TButton9
            txtLocation.Text = TLoc9
        End If
        'Shows settings for Button 10
        If cmbButtons.Text = "Button 10" Then
            txtName.Text = TButton10
            txtLocation.Text = TLoc10
        End If
        'Shows settings for Button 11
        If cmbButtons.Text = "Button 11" Then
            txtName.Text = TButton11
            txtLocation.Text = TLoc11
        End If
        'Shows settings for Button 12
        If cmbButtons.Text = "Button 12" Then
            txtName.Text = TButton12
            txtLocation.Text = TLoc12
        End If
        'Shows settings for Button 13
        If cmbButtons.Text = "Button 13" Then
            txtName.Text = TButton13
            txtLocation.Text = TLoc13
        End If
        'Shows settings for Button 14
        If cmbButtons.Text = "Button 14" Then
            txtName.Text = TButton14
            txtLocation.Text = TLoc14
        End If
        'Shows settings for Button 15
        If cmbButtons.Text = "Button 15" Then
            txtName.Text = TButton15
            txtLocation.Text = TLoc15
        End If
    End Sub

    Private Sub btnOpenFile2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFile2.Click
        'Shows the file browser.
        OpenFileDialog1.ShowDialog()
        'Adds the location to txtLocation
        txtLocation.Text = OpenFileDialog1.FileName
    End Sub

    Private Sub chkDisabled_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDisabled.CheckedChanged
        'If checked than this button is disabled.
        If cmbButtons.Text = "1" Then
            If chkDisabled.Checked = True Then
                frmReela.Button1.Enabled = False
            Else
                frmReela.Button1.Enabled = True
            End If
        End If
        'If checked than this button is disabled.
        If cmbButtons.Text = "2" Then
            If chkDisabled.Checked = True Then
                frmReela.Button2.Enabled = False
            Else
                frmReela.Button2.Enabled = True
            End If
        End If
        'If checked than this button is disabled.
        If cmbButtons.Text = "3" Then
            If chkDisabled.Checked = True Then
                frmReela.Button3.Enabled = False
            Else
                frmReela.Button3.Enabled = True
            End If
        End If
        'If checked than this button is disabled.
        If cmbButtons.Text = "4" Then
            If chkDisabled.Checked = True Then
                frmReela.Button4.Enabled = False
            Else
                frmReela.Button4.Enabled = True
            End If
        End If
        'If checked than this button is disabled.
        If cmbButtons.Text = "5" Then
            If chkDisabled.Checked = True Then
                frmReela.Button5.Enabled = False
            Else
                frmReela.Button5.Enabled = True
            End If
        End If
        'If checked than this button is disabled.
        If cmbButtons.Text = "6" Then
            If chkDisabled.Checked = True Then
                frmReela.Button6.Enabled = False
            Else
                frmReela.Button6.Enabled = True
            End If
        End If
        'If checked than this button is disabled.
        If cmbButtons.Text = "7" Then
            If chkDisabled.Checked = True Then
                frmReela.Button7.Enabled = False
            Else
                frmReela.Button7.Enabled = True
            End If
        End If
        'If checked than this button is disabled.
        If cmbButtons.Text = "8" Then
            If chkDisabled.Checked = True Then
                frmReela.Button8.Enabled = False
            Else
                frmReela.Button8.Enabled = True
            End If
        End If
        'If checked than this button is disabled.
        If cmbButtons.Text = "9" Then
            If chkDisabled.Checked = True Then
                frmReela.Button9.Enabled = False
            Else
                frmReela.Button9.Enabled = True
            End If
        End If
        'If checked than this button is disabled.
        If cmbButtons.Text = "10" Then
            If chkDisabled.Checked = True Then
                frmReela.Button10.Enabled = False
            Else
                frmReela.Button10.Enabled = True
            End If
        End If
        'If checked than this button is disabled.
        If cmbButtons.Text = "11" Then
            If chkDisabled.Checked = True Then
                frmReela.Button11.Enabled = False
            Else
                frmReela.Button11.Enabled = True
            End If
        End If
        'If checked than this button is disabled.
        If cmbButtons.Text = "12" Then
            If chkDisabled.Checked = True Then
                frmReela.Button12.Enabled = False
            Else
                frmReela.Button12.Enabled = True
            End If
        End If
        'If checked than this button is disabled.
        If cmbButtons.Text = "13" Then
            If chkDisabled.Checked = True Then
                frmReela.Button13.Enabled = False
            Else
                frmReela.Button13.Enabled = True
            End If
        End If
        If cmbButtons.Text = "14" Then
            If chkDisabled.Checked = True Then
                frmReela.Button14.Enabled = False
            Else
                frmReela.Button14.Enabled = True
            End If
        End If
        'If checked than this button is disabled.
        If cmbButtons.Text = "15" Then
            If chkDisabled.Checked = True Then
                frmReela.Button15.Enabled = False
            Else
                frmReela.Button15.Enabled = True
            End If
        End If
    End Sub


    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        'SETTINGS
        objIniFile.WriteString("Settings", "Image", txtImage.Text)
        objIniFile.WriteString("Settings", "Title", txtTitle.Text)

        frmReela.Text = txtTitle.Text
        Me.Text = txtTitle.Text & " - Settings"
        frmLogin.Text = txtTitle.Text & " - Login"

        If txtImage.Text = "" Then
            frmReela.imgTopLogo.Image = Reela.My.Resources.Resources.reela
            Me.imgTopLogo.Image = Reela.My.Resources.Resources.reela
            frmLogin.imgTopLogo.Image = Reela.My.Resources.Resources.reela
        Else
            frmReela.imgTopLogo.Image = System.Drawing.Image.FromFile(Image)
            Me.imgTopLogo.Image = System.Drawing.Image.FromFile(Image)
            frmLogin.imgTopLogo.Image = System.Drawing.Image.FromFile(Image)
        End If

        Dim fileExists2 As Boolean
        fileExists2 = My.Computer.FileSystem.FileExists(txtINI.Text)
        If fileExists2 = False Then
            MsgBox("This file does not exist. Please enter the correct file.")
            Exit Sub
        End If

        'GROUP SETTINGS
        Dim fileExists As Boolean
        Dim folderExists As Boolean
        fileExists = My.Computer.FileSystem.FileExists(txtLocation.Text)
        folderExists = My.Computer.FileSystem.DirectoryExists(txtLocation.Text)

        If fileExists = False Then
        Else
            If folderExists = False Then
                MsgBox("This folder/file does not exist." & vbCrLf & txtLocation.Text, MsgBoxStyle.Critical)
                Exit Sub
            End If
        End If

        objIniFile.WriteString("Names", "Button" & cmbButtons.Text, txtName.Text)
        objIniFile.WriteString("Locations", "Button" & cmbButtons.Text & "_loc", txtLocation.Text)

        If cmbButtons.Text = "1" Then
            frmReela.Button1.Text = txtName.Text

        End If
        If cmbButtons.Text = "2" Then
            frmReela.Button2.Text = txtName.Text

        End If
        If cmbButtons.Text = "3" Then
            frmReela.Button3.Text = txtName.Text

        End If
        If cmbButtons.Text = "4" Then
            frmReela.Button4.Text = txtName.Text

        End If
        If cmbButtons.Text = "5" Then
            frmReela.Button5.Text = txtName.Text

        End If
        If cmbButtons.Text = "6" Then
            frmReela.Button6.Text = txtName.Text

        End If
        If cmbButtons.Text = "7" Then
            frmReela.Button7.Text = txtName.Text

        End If
        If cmbButtons.Text = "8" Then
            frmReela.Button8.Text = txtName.Text

        End If
        If cmbButtons.Text = "9" Then
            frmReela.Button9.Text = txtName.Text

        End If
        If cmbButtons.Text = "10" Then
            frmReela.Button10.Text = txtName.Text

        End If
        If cmbButtons.Text = "11" Then
            frmReela.Button11.Text = txtName.Text

        End If
        If cmbButtons.Text = "12" Then
            frmReela.Button12.Text = txtName.Text

        End If
        If cmbButtons.Text = "13" Then
            frmReela.Button13.Text = txtName.Text

        End If
        If cmbButtons.Text = "14" Then
            frmReela.Button14.Text = txtName.Text

        End If
        If cmbButtons.Text = "15" Then
            frmReela.Button15.Text = txtName.Text
        End If

        'Saves INI location.
        If txtINI.Text = My.Settings.INI_Location Then
        Else
            My.Settings.INI_Location = txtINI.Text
            MsgBox("Please restart Reela to start using it.", MsgBoxStyle.Information, "Please restart")
            frmReela.Close()
        End If

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntClose.Click
        'Closes this window.
        Me.Close()
    End Sub

    Private Sub frmChange_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim fileContents As String
        Dim version As String
        Dim fileExists As Boolean
        'Checks for latest version file.
        fileExists = My.Computer.FileSystem.FileExists("C:\version.txt")
        If fileExists = True Then
            fileContents = My.Computer.FileSystem.ReadAllText("C:\version.txt")
        Else
            VLatest = "null"
        End If
        'Loads version information.
        version = System.Windows.Forms.Application.ProductVersion
        lblVersion.Text = VCurrent

        'Loads title
        txtTitle.Text = Title
        txtImage.Text = Image
        txtINI.Text = My.Settings.INI_Location

        'Checks for ''Hide from taskbar''
        If My.Settings.Hide = True Then
            chkHideSettings.Checked = False
        Else
            chkHideSettings.Checked = True
        End If

        Me.ShowInTaskbar = My.Settings.Hide

        'Shows version information
        If VLatest = "null" Then
            lblUpdate.Text = "Error!"
            lblUpdate.ForeColor = Color.Yellow
            btnUpdate.Visible = False
        Else
            If version <> VLatest Then
                lblUpdate.Text = "New version available."
                lblUpdate.ForeColor = Color.OrangeRed
                btnUpdate.Visible = True
            Else
                lblUpdate.Text = "Version OK!"
                lblUpdate.ForeColor = Color.Lime
                btnUpdate.Visible = False
            End If
        End If
        'Loads title.
        Me.Text = Title & " - Settings"
        If Image <> "" Then
            imgTopLogo.Image = Reela.My.Resources.Resources.reela
        Else
            Exit Sub
        End If
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        System.Diagnostics.Process.Start("http://www.retroplay.nl/Site/index.php?option=com_content&task=view&id=8&Itemid=57")
    End Sub

    Private Sub btnOpenFile3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFile3.Click
        'Opens filebrowser for the TopLogo image.
        OpenFileDialog1.ShowDialog()
        txtINI.Text = OpenFileDialog1.FileName
    End Sub
End Class

Public Class IniFile
    ' API functions
    Private Declare Ansi Function GetPrivateProfileString _
    Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
    (ByVal lpApplicationName As String, _
    ByVal lpKeyName As String, ByVal lpDefault As String, _
    ByVal lpReturnedString As System.Text.StringBuilder, _
    ByVal nSize As Integer, ByVal lpFileName As String) _
    As Integer
    Private Declare Ansi Function WritePrivateProfileString _
    Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
    (ByVal lpApplicationName As String, _
    ByVal lpKeyName As String, ByVal lpString As String, _
    ByVal lpFileName As String) As Integer
    Private Declare Ansi Function GetPrivateProfileInt _
    Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _
    (ByVal lpApplicationName As String, _
    ByVal lpKeyName As String, ByVal nDefault As Integer, _
    ByVal lpFileName As String) As Integer
    Private Declare Ansi Function FlushPrivateProfileString _
    Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
    (ByVal lpApplicationName As Integer, _
    ByVal lpKeyName As Integer, ByVal lpString As Integer, _
    ByVal lpFileName As String) As Integer
    Dim strFilename As String

    ' filename
    Public Sub New(ByVal Filename As String)
        strFilename = Filename
    End Sub

    Public Function GetString(ByVal Section As String, ByVal Key As String, ByVal [Default] As String) As String
        ' Returns a string from your INI file
        Dim intCharCount As Integer
        Dim objResult As New System.Text.StringBuilder(256)
        intCharCount = GetPrivateProfileString(Section, Key, _
        [Default], objResult, objResult.Capacity, strFilename)
        If intCharCount > 0 Then
            GetString = Left(objResult.ToString, intCharCount)
        End If
    End Function

    Public Function GetInteger(ByVal Section As String, ByVal Key As String, ByVal [Default] As Integer) As Integer
        ' Returns an integer from your INI file
        Return GetPrivateProfileInt(Section, Key, _
        [Default], strFilename)
    End Function

    Public Sub WriteString(ByVal Section As String, ByVal Key As String, ByVal Value As String)
        ' Writes a string to your INI file
        WritePrivateProfileString(Section, Key, Value, strFilename)
        Flush()
    End Sub

    Public Sub WriteInteger(ByVal Section As String, ByVal Key As String, ByVal Value As Integer)
        ' Writes an integer to your INI file
        WriteString(Section, Key, CStr(Value))
        Flush()
    End Sub

    Private Sub Flush()
        ' Stores all the cached changes to your INI file
        FlushPrivateProfileString(0, 0, 0, strFilename)
    End Sub

End Class